package com.ztom.offer.v2;

import java.util.Deque;
import java.util.LinkedList;

/**
 * 用两个栈实现队列
 * <p>
 * https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/
 *
 * @author ZhangTao
 */
public class Code06CQueue {

    class CQueue {

        private Deque<Integer> inStack;
        private Deque<Integer> outStack;

        public CQueue() {
            inStack = new LinkedList<>();
            outStack = new LinkedList<>();
        }

        public void appendTail(int value) {
            inStack.offerLast(value);
        }

        public int deleteHead() {
            // 输出栈为空, 取输入栈
            if (outStack.isEmpty()) {
                if (inStack.isEmpty()) {
                    return -1;
                }
                in2out();
            }
            return outStack.pollLast();
        }

        private void in2out() {
            while (!inStack.isEmpty()) {
                outStack.offerLast(inStack.pollLast());
            }
        }
    }
}
